Seata分布式事务(AT模式)粗讲

1.nacos服务

  1. 下载 nacos-server 服务
  1. 解压文件后进入到bin目录运行命令 startup.cmd -m standalone启动nacos服务

2.seata服务

  1. 下载 seata-server 服务
  2. nacos 服务中创建seata命令空间(ps:命名空间ID定义为seata)
  1. 通过脚本 nacos-config.sh 将配置文件config.txt以key作为配置ID,value为文本的配置导入nacos的seata命名空间。配置内容如下:
1
2
3
4
5
6
7
8
9
10
# seata 客户端配置 (ps:seata-text 是事务分组名称可以修改,但是要与服务端tx-service-group值对应,seata 是seata服务集群的名称)
service.vgroupMapping.seata-text=seata
# seata 服务存储配置
store.mode=db
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://xxx.xxx.xxx.xxx:3306/seata?useUnicode=true&rewriteBatchedStatements=true
store.db.user=username
store.db.password=password

ps: 数据库的地址、用户名和密码按步骤3配置的信息修改,仓库名默认seata。使用命令sh nacos-config.sh -h 127.0.0.1 -p 8848 -g SEATA_GROUP -t seata -u nacos -w nacos
配置将配置导入 nacos 服务中

  1. 修改seata服务conf目录下的registry.conf文件内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "nacos"
loadBalance = "RandomLoadBalance"
loadBalanceVirtualNodes = 10

nacos {
application = "seata-server"
serverAddr = "127.0.0.1:8848"
group = "SEATA_GROUP"
namespace = "seata"
cluster = "seata"
username = "nacos"
password = "nacos"
}
}

config {
# file、nacos 、apollo、zk、consul、etcd3
type = "nacos"
nacos {
serverAddr = "xxx.xxx.xxx.xxx:8848"
namespace = "seata"
group = "SEATA_GROUP"
username = "nacos"
password = "nacos"
}

}

127.0.0.1修改成nacos注册中心地址,执行bin目录下的seata-server.sh启动seata服务

1
2
> ./seata-server.sh -p 8091 -h 127.0.0.1
>

整个seata目录内容如下:

image-20210420214024507

3.demo测试

  1. 下载seata-demo测试用例:https://github.com/luxiaowan/cc-springboot-family.git

    cc-nacos-client1
    cc-nacos-client2
    cc-nacos-client3
    cc-spring-boot-seata

  1. 创建db_accountdb_orderdb_storage脚本如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
create schema db_account;

create table db_account.account_tbl (
id int auto_increment primary key,
user_id varchar(255) null,
money int default 0 null
) charset=utf8;

create table db_account.undo_log (
id bigint auto_increment primary key,
branch_id bigint not null,
xid varchar(100) not null,
context varchar(128) not null,
rollback_info longblob not null,
log_status int not null,
log_created datetime not null,
log_modified datetime not null,
ext varchar(100) null,
constraint ux_undo_log unique (xid, branch_id)
) charset=utf8;

INSERT INTO db_account.account_tbl (id, user_id, money) VALUES (1, '1001', 10000);
INSERT INTO db_account.account_tbl (id, user_id, money) VALUES (2, '1002', 10000);

create schema db_order;

create table db_order.order_tbl (
id int auto_increment primary key,
user_id varchar(255) null,
commodity_code varchar(255) null,
count int default 0 null,
money int default 0 null
) charset=utf8;

create table db_order.undo_log (
id bigint auto_increment primary key,
branch_id bigint not null,
xid varchar(100) not null,
context varchar(128) not null,
rollback_info longblob not null,
log_status int not null,
log_created datetime not null,
log_modified datetime not null,
ext varchar(100) null,
constraint ux_undo_log unique (xid, branch_id)
) charset=utf8;


create schema db_storage;

create table db_storage.storage_tbl (
id int auto_increment primary key,
commodity_code varchar(255) null,
count int default 0 null,
price decimal(10,2) default 0.00 null comment '单价',
constraint commodity_code unique (commodity_code)
) charset=utf8;

create table db_storage.undo_log (
id bigint auto_increment primary key,
branch_id bigint not null,
xid varchar(100) not null,
context varchar(128) not null,
rollback_info longblob not null,
log_status int not null,
log_created datetime not null,
log_modified datetime not null,
constraint ux_undo_log unique (xid, branch_id)
) charset=utf8;

INSERT INTO db_storage.storage_tbl (id, commodity_code, count, price) VALUES (1, '2001', 900, 2.00);


create schema seata;
create table seata.undo_log (
id bigint auto_increment primary key,
branch_id bigint not null,
xid varchar(100) not null,
context varchar(128) not null,
rollback_info longblob not null,
log_status int not null,
log_created datetime not null,
log_modified datetime not null,
constraint ux_undo_log unique (xid, branch_id)
) charset=utf8;
  1. 启动各服务,只要在开始全局事务的地方加上注解@GlobalTransactional就开启AT模式的分布式事务